import request from '@utils/request';
import * as actionTypes from './actionType'
import { message } from 'antd';
import moment from 'moment';

//  获取所有操作日志数据
export const getAllLog = (page) => {
    return (dispatch) => {
        request('POST','/api/server/web/rest/log/getAllLog', {
            cmd: 'getAllLog',
            type: 'request',
            request: {
                ...page
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount=res.totalCount;
                let result = res.message;
                // let data =modifyingtimestamps(result)
                dispatch(AllLogUpdateReducer(result,totalCount))
            } else {
                message.error('获取所有操作日志失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
//根据id查看详情
export const getLogByIdRequest = (id) => {
    return (dispatch) => {
        request('POST', '/api/serverweb/rest/log/getLogById', {
            cmd: 'getLogById',
            type: 'request',
            request: {
                id
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                dispatch(getAllLog(result))
            } else {
                message.error('查询失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

const AllLogUpdateReducer = (allLog,totalCount) => ({
    type: actionTypes.GETALLLOG,
    allLog,
    totalCount
})


//根据开始时间和结束时间查询日志
export const getLogByTimeRequest = (information) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/log/getLogByTime', {
            cmd: 'getLogByTime',
            type: 'request',
            request: {
                ...information
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let totalCount=res.totalCount
                let result = res.message;
                let data = modifyingtimestamps(result);
                message.success('查找成功', 1);
                dispatch((AllLogUpdateReducer(data,totalCount)))
            } else {
                message.error('查找失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

//将时间戳改成正常时间
const modifyingtimestamps =(data)=>{
    let array =data

    for(var i=0;i<data.length;i++){
        array[i].operationTime=moment(data[i].operationTime).format('YYYY-MM-DD HH:mm')
    }
    return array
}